package com.onenine.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.onenine.app.R;
import com.onenine.app.activity.BrowserImageActivity;
import com.onenine.app.activity.UserDetailActivity;
import com.onenine.app.model.BarrageItem;
import com.onenine.app.utils.CHelper;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.GlideUtils;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.app.view.NVoiceBar;
import com.onenine.base.view.NiceImageView;
import com.onenine.chat.util.SimpleCommonUtils;
import com.onenine.common.util.ApUtils;
import com.onenine.common.util.SizeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BarrageCateAdapter extends BaseMultiItemQuickAdapter<BarrageItem, BaseViewHolder> {

    private int mAvatarSize;
    private int mImgSize;
    private Context mContext;

    public BarrageCateAdapter(Context context, List<BarrageItem> data) {
        super(data);
        mContext = context;
        mAvatarSize = SizeUtils.dp2px(50);
        mImgSize = SizeUtils.dp2px(74);
        addItemType(0, R.layout.layout_barrage_popup_text);
        addItemType(DataType.BARRAGE_TEXT, R.layout.layout_barrage_popup_text);
        addItemType(DataType.BARRAGE_IMAGE, R.layout.layout_barrage_popup_img);
        addItemType(DataType.BARRAGE_IMAGE_TEXT, R.layout.layout_barrage_popup_img);
        addItemType(DataType.BARRAGE_VOICE, R.layout.layout_barrage_popup_voice);
        addItemType(DataType.BARRAGE_VOICE_TEXT, R.layout.layout_barrage_popup_voice);
    }

    @Override
    protected void convert(BaseViewHolder holder, BarrageItem item) {
        switch (holder.getItemViewType()) {
            case DataType.BARRAGE_TEXT:
                handleCommonInfo(holder, item);
                break;
            case DataType.BARRAGE_IMAGE:
            case DataType.BARRAGE_IMAGE_TEXT:
                handleCommonInfo(holder, item);
                handleImageInfo(holder, item.getPictureUrl());
                break;
            case DataType.BARRAGE_VOICE:
            case DataType.BARRAGE_VOICE_TEXT:
                handleCommonInfo(holder, item);
                handleVoiceInfo(holder, item.getSex(), item.getVoiceUrl());
                break;
        }
    }

    /**
     * 处理音频信息
     *
     * @param holder
     * @param voiceUrl
     */
    private void handleVoiceInfo(BaseViewHolder holder, int sex, String voiceUrl) {
        NVoiceBar voiceBar = holder.getView(R.id.nv_voice);
        voiceBar.setData(voiceUrl, 0);
        voiceBar.setSexStyle(sex, true);
    }

    /**
     * 处理图片信息
     *
     * @param holder
     * @param pictureUrl
     */
    private void handleImageInfo(BaseViewHolder holder, String pictureUrl) {
        String[] picUrls = pictureUrl.split(",");
        LinearLayout llImg = holder.getView(R.id.ll_img);
        llImg.removeAllViews();
        for (int i = 0; i < picUrls.length; i++) {
            NiceImageView ivImg = new NiceImageView(mContext);
            ivImg.setCornerRadius(8);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mImgSize, mImgSize);
            lp.leftMargin = lp.rightMargin = lp.topMargin
                    = lp.bottomMargin = SizeUtils.dp2px(6);
            llImg.addView(ivImg, lp);
            GlideUtils.loadSrcImage(ApUtils.getApp(), ivImg, CHelper.getOssThumbImageUrl(picUrls[i], mImgSize));
            final int index = i;
            ivImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BrowserImageActivity.launchActivity(mContext
                            , new ArrayList<>(Arrays.asList(picUrls)), index);
                }
            });
        }
    }

    /**
     * 处理普通文本信息
     *
     * @param holder
     * @param item
     */
    private void handleCommonInfo(BaseViewHolder holder, BarrageItem item) {
        TextView tvName = holder.getView(R.id.tv_name);
        tvName.setText(item.getNickName());
        ImageView ivAvatar = holder.getView(R.id.aiv_avatar);
        GlideUtils.loadSrcImage(ApUtils.getApp(), ivAvatar, CHelper.getOssThumbImageUrl(item.getPhoto(), mAvatarSize));
        holder.setImageResource(R.id.iv_sex, RoleResLoader.getRoleDrawableResourceId(RoleResLoader
                .DynamicResName.COMMUNITY_SEX_ICON, item.getSex()));
        TextView tvDesc = holder.getView(R.id.tv_content);
        if (TextUtils.isEmpty(item.getContent())) {
            tvDesc.setVisibility(View.GONE);
        } else {
            tvDesc.setVisibility(View.VISIBLE);
            SimpleCommonUtils.spannableEmoticonFilter(tvDesc, item.getContent());
        }
        ivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserDetailActivity.launchActivity(mContext, item.getUserId());
            }
        });
        TextView tvLoc = holder.getView(R.id.tv_location);
        if (TextUtils.isEmpty(item.getCityName())) {
            tvLoc.setVisibility(View.GONE);
        } else {
            tvLoc.setVisibility(View.VISIBLE);
            tvLoc.setText(item.getCityName());
            tvLoc.setCompoundDrawablesWithIntrinsicBounds(RoleResLoader.getRoleDrawableResourceId(RoleResLoader.DynamicResName.MINE_ADDR_ICON
                    , item.getSex()), 0, 0, 0);
        }
    }

}
